<template>
  <div>
    <el-form ref="form" label-width="80px">
      <el-form-item label="spu名称">
        {{ spu.spuName }}
      </el-form-item>
      <el-form-item label="sku名称">
        <el-input v-model="skuInfo.skuName" placeholder="sku名称"></el-input>
      </el-form-item>
      <el-form-item label="价格（元）">
        <el-input v-model="skuInfo.price" placeholder="价格（元素）" type="number"></el-input>
      </el-form-item>
      <el-form-item label="重量（千克）">
        <el-input v-model="skuInfo.weight" placeholder="重量（千克）"></el-input>
      </el-form-item>
      <el-form-item label="规格描述">
        <el-input v-model="skuInfo.skuDesc" placeholder="规格描述" rows="4" type="textarea"></el-input>
      </el-form-item>
      <el-form-item label="平台属性">
        <el-form ref="form" :inline="true" label-width="80px">
          <el-form-item v-for="(attr, index) in attrInfoList" :key="attr.id" :label="attr.attrName">
            <el-select v-model="attr.attrIdAndValueId" placeholder="请选择">
              <el-option v-for="(attrValue, index) in attr.attrValueList" :key="attrValue.id"
                         :label="attrValue.valueName" :value="`${attr.id}:${attrValue.id}`"
              ></el-option>
            </el-select>
          </el-form-item>
        </el-form>
      </el-form-item>
      <el-form-item label="销售属性">
        <el-form ref="form" :inline="true" label-width="80px">
          <el-form-item v-for="(saleAttr, index) in spuSaleAttrList" :key="saleAttr.id" :label="saleAttr.saleAttrName">
            <el-select v-model="saleAttr.attrIdAndValueId" placeholder="请选择">
              <el-option v-for="(saleAttrValue, index) in saleAttr.spuSaleAttrValueList" :key="saleAttrValue.id"
                         :label="saleAttrValue.saleAttrValueName"
                         :value="`${saleAttr.id}:${saleAttrValue.id}`"
              ></el-option>
            </el-select>
          </el-form-item>
        </el-form>
      </el-form-item>
      <el-form-item label="图片列表">
        <el-table
          :data="spuImageList"
          border
          style="width: 100%"
          @selection-change="handleSelectionChange"
        >
          <el-table-column
            type="selection"
            width="80"
          >
          </el-table-column>
          <el-table-column
            #default="{row, $index}"
            label="图片"
            prop="name"
          >
            <img :src="row.imgUrl" style="width: 100px; height: 100px">
          </el-table-column>
          <el-table-column
            label="名称"
            prop="imgName"
          >
          </el-table-column>
          <el-table-column
            #default="{row, $index}"
            label="操作"
            prop="address"
          >
            <el-button v-if="row.isDefault == 0" type="primary" @click="changeDefault(row)">设置默认</el-button>
            <el-button v-else>默认</el-button>

          </el-table-column>
        </el-table>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="save">保存</el-button>
        <el-button @click="cancel">取消</el-button>

      </el-form-item>
    </el-form>
  </div>
</template>

<script>
export default {
  name: 'SkuForm',
  data() {
    return {
      spuImageList: [],
      spuSaleAttrList: [],
      attrInfoList: [],
      skuInfo: {
        'category3Id': 0,
        spuId: 0,
        tmId: 0,

        'price': 0,
        'weight': '',
        'skuName': '',
        'skuDesc': '',

        'skuAttrValueList': [
          // {
          //   'attrId': 0,
          //   'valueId': 0,
          // }
        ],
        'skuDefaultImg': '',
        'skuImageList': [
          // {
          //   'id': 0,
          //   'imgName': 'string',
          //   'imgUrl': 'string',
          //   'isDefault': 'string',
          //   'skuId': 0,
          //   'spuImgId': 0
          // }
        ],
        'skuSaleAttrValueList': [
          // {
          //   'id': 0,
          //   'saleAttrId': 0,
          //   'saleAttrName': 'string',
          //   'saleAttrValueId': 0,
          //   'saleAttrValueName': 'string',
          //   'skuId': 0,
          //   'spuId': 0
          // }
        ]
      },
      spu: {},
      imageList: []
    }
  },
  methods: {
    async getData(category1Id, category2Id, spu) {
      this.skuInfo.category3Id = spu.category3Id
      this.skuInfo.spuId = spu.id
      this.skuInfo.tmId = spu.tmId
      this.spu = spu

      let result = await this.$API.spu.reqSpuImageList(spu.id)
      if (result.code == 200) {
        let list = result.data
        list.forEach(item => {
          item.isDefault = 0
        })
        this.spuImageList = list
      }

      let result1 = await this.$API.spu.reqSpuSaleAttrList(spu.id)
      if (result1.code == 200) {
        this.spuSaleAttrList = result1.data
      }

      let result2 = await this.$API.spu.reqAttrInfoList(category1Id, category2Id, spu.category3Id)
      if (result2.code == 200) {
        this.attrInfoList = result2.data
      }
    },
    handleSelectionChange(params) {
      this.imageList = params
    },
    changeDefault(row) {
      this.spuImageList.forEach(item => {
        item.isDefault = 0
      })
      row.isDefault = 1
      this.skuInfo.skuDefaultImg = row.imgUrl
    },
    cancel() {
      this.$emit('changeScene', 0)
      Object.assign(this.$data, this.$options.data())
    },
    async save() {
      const { attrInfoList, skuInfo, spuSaleAttrList, imageList } = this

      // attrInfoList.forEach(item => {
      //   if (item.attrIdAndValueId) {
      //     let [attrId, valueId] = item.attrIdAndValueId.split(':')
      //     skuInfo.skuAttrValueList.push({
      //       attrId, valueId
      //     })
      //   }
      // })

      spuSaleAttrList.forEach(item => {
        if (item.attrIdAndValueId) {
          let [saleAttrId, saleAttrValueId] = item.attrIdAndValueId.split(':')
          skuInfo.skuSaleAttrValueList.push({
            saleAttrId, saleAttrValueId
          })
        }
      })

      skuInfo.skuImageList = imageList.map(item => {
        return {
          imgName: item.imgName,
          imgUrl: item.imgUrl,
          isDefault: item.isDefault,
          spuImgId: item.id
        }
      })

      skuInfo.attrInfoList = attrInfoList.reduce((prev, item) => {
        if (item.attrIdAndValueId) {
          let [attrId, valueId] = item.attrIdAndValueId.split(':')
          prev.push({
            attrId, valueId
          })
        }
        return prev

      }, [])

      let result = await this.$API.spu.reqAddSku(skuInfo)
      if (result.code == 200) {
        this.$message({ type: 'success', message: '添加sku成功' })
        Object.assign(this._data, this.$options.data())
        this.$emit('changeScene', 0)
      }
      // skuInfo.skuSaleAttrValueList = spuSaleAttrList.reduce((prev, item) => {
      //   if (item.attrIdAndValueId) {
      //     let [saleAttrId, saleAttrValueId] = item.attrIdAndValueId.split(':')
      //     prev.push({
      //       saleAttrId, saleAttrValueId
      //     })
      //     return prev
      //   }
      // }, [])
    }
  }
}
</script>

<style scoped>

</style>
